package com.ysx.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 * request 请求对象
 * response 应答对象
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //获取session对象
        Object obj = request.getSession().getAttribute("user");
//        判断session里面是否有用户，没有的话重定向到登录页面，给拦截掉
        if (obj == null) {
            response.sendRedirect("/admin");
            return false;
        }
        return true;
    }
}